#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------
# --
# ****************************
# Author: lmay.Zhou
# Blog: www.lmaye.com
# Date: 2021/11/2 15:43
# Email lmay@lmaye.com
# ----------------------------------------------------------
import pandas as pd
import tushare
from apscheduler.schedulers.blocking import BlockingScheduler


def realtime_quotes(codes_, columns):
    """
        实时行情

        :param codes_:  代码
        :param columns: 列名
        :return: DataFrame
    """
    quotes = tushare.get_realtime_quotes(codes_)
    data_list = []
    for it in quotes.itertuples():
        row_data = []
        for item in columns:
            if "amplitude" == item:
                diff = float(getattr(it, "price")) - float(getattr(it, "open"))
                row_data.append(str(round(diff / float(getattr(it, "open")) * 100, 2)) + "%")
                break
            row_data.append(getattr(it, item))
        data_list.append(row_data)
    df = pd.DataFrame(data_list, columns=columns)
    print(df.to_string())


def task():
    codes = ["600111", "002176", "600010", "601868"]
    clos = ['code', 'name', 'open', 'price', 'high', 'low', 'bid', 'ask', 'volume', 'amount', 'date', 'time',
            'pre_close', 'amplitude']
    scheduler = BlockingScheduler(timezone="Asia/Shanghai")
    scheduler.add_job(realtime_quotes, args=(codes, clos), trigger='interval', seconds=2)
    scheduler.start()


if __name__ == "__main__":
    task()
